package fr.inserm.um915.bdibd.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;


public class Family
	{
	private String name;
	private List<Individual> individuals=new ArrayList<Individual>();
	private Map<String, Individual> name2individual=new TreeMap<String, Individual>();
	private Map<Integer, Individual> id2individual=new TreeMap<Integer, Individual>();
	

	
	public Family(String name)
		{
		this.name=name;
		}
	
	public String getName()
		{
		return name;
		}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + name.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj)
		{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Family other = (Family) obj;
		return name.equals(other.name);
		}

	public void add(Individual i)
		{
		if(i==null) throw new NullPointerException();
		if(name2individual.containsKey(i.getName())) throw new RuntimeException("duplicate individual "+i.getName());
		name2individual.put(i.getName(), i);
		id2individual.put(i.getId(), i);
		individuals.add(i);
		}
	
	public List<Individual> getIndividuals()
		{
		return Collections.unmodifiableList(this.individuals);
		}
	
	public Individual getIndividualByName(String s)
		{
		return name2individual.get(s);
		}
	public Individual getIndividualById(int id)
		{
		return id2individual.get(id);
		}

	@Override
	public String toString() {
		return name;
		}
	}
